package ece1779.ec2;

import java.io.*;
import java.util.List;

import javax.servlet.*;
import javax.servlet.http.*;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3ObjectSummary;


public class DeleteBucket extends HttpServlet {
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException {
		doPost(request,response);
	}
	
    public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException {
        try {

            PrintWriter out = response.getWriter();

        	response.setContentType("text/html");
            
            out.write("<html><head><title>Sample Image Upload</title></head>");
            out.write("<body>");

            
        	BasicAWSCredentials awsCredentials = (BasicAWSCredentials)this.getServletContext().getAttribute("AWSCredentials");

        	AmazonS3 s3 = new AmazonS3Client(awsCredentials);
            
            String bucketName = request.getParameter("bucketName");
     


            try {
            	
            	ObjectListing objects = s3.listObjects(bucketName);

           		List<S3ObjectSummary> summaries = objects.getObjectSummaries();
        		for (S3ObjectSummary item : summaries) {
        			String key = item.getKey();
        			s3.deleteObject(bucketName, key);
        			out.println("<p>Deleted " + key +"</p>");
            		out.flush();
        		}
            	
            	while (objects.isTruncated()) {
            		objects = s3.listNextBatchOfObjects(objects);
            		summaries = objects.getObjectSummaries();
            		for (S3ObjectSummary item : summaries) {
            			String key = item.getKey();
            			s3.deleteObject(bucketName, key);
            			out.println("<p>Deleted " + key +"</p>");
                		out.flush();
            		}
            	}

            	s3.deleteBucket(bucketName);
            	out.println("<p>Deleted Bucket " + bucketName + "</p>");
        		
            	
            } catch (AmazonServiceException ase) {
                out.println("Caught an AmazonServiceException, which means your request made it "
                        + "to Amazon S3, but was rejected with an error response for some reason.");
                out.println("Error Message:    " + ase.getMessage());
                out.println("HTTP Status Code: " + ase.getStatusCode());
                out.println("AWS Error Code:   " + ase.getErrorCode());
                out.println("Error Type:       " + ase.getErrorType());
                out.println("Request ID:       " + ase.getRequestId());
            } catch (AmazonClientException ace) {
                out.println("Caught an AmazonClientException, which means the client encountered "
                        + "a serious internal problem while trying to communicate with S3, "
                        + "such as not being able to access the network.");
                out.println("Error Message: " + ace.getMessage());
            }
 
            
            
                       
            
            
            out.write("</body></html>");
             
	}
	catch (Exception ex) {
	    throw new ServletException (ex);
	}
	
    }
    

    
    
}